// jshint esversion: 6
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
const extractLess = new ExtractTextPlugin({
  filename: '[name].css',
  disable: process.env.NODE_ENV === 'development'
});
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

var path = require('path');

module.exports = {
  entry: {
    main: path.resolve(__dirname, path.join('09-webpack', 'app', 'script', 'index')),
    vendor: ['react', 'react-dom']
  },
  output: {
    path: path.resolve(__dirname, path.join('09-webpack', 'dist')),
    filename: '[name].js'
  },
  module: {
    rules: [{
      test: /\.(css|less)$/,
      use: extractLess.extract({
        use: [{
          loader: 'css-loader'
        }, {
          loader: 'less-loader', options:{'compress': true}
        }],
        // use style-loader in development
        fallback: 'style-loader'
      })
    }, { // ------------------------------- .jsx 打包规则
      test: /.(js|jsx)?$/,
      include: [
        path.resolve(__dirname, path.join('09-webpack', 'app', 'script'))
      ],
      exclude: [
        path.resolve(__dirname, 'node_modules')
      ],
      loader: 'babel-loader'
    }, ]
  },
  resolve: {
    extensions: ['.json', '.js', '.jsx', '.css', '.less']
  },
  devtool: 'source-map',
  devServer: {
    publicPath: path.join('/dist/')
  },
  plugins: [
    extractLess,
    new webpack.optimize.CommonsChunkPlugin({
      names: ['vendor', 'runtime']
    }),
    new UglifyJSPlugin()
  ],
  // externals: {
  //   'react': 'React',
  //   'react-dom': 'ReactDOM'
  // }
};
